package com.mybatis.boost.generator.java;

import com.google.common.base.Joiner;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Map;

import static com.google.common.collect.Maps.newHashMap;
import static com.mybatis.boost.tool.StringUtil.trim;

/**
 * @author jearton
 * @since 2017/2/4
 */
@Getter
@NoArgsConstructor
public class Annotation {
    private String name;

    private Map<String, String> properties = newHashMap();

    public Annotation(String name) {
        this.name = trim(name);
    }

    public Annotation(String name, Map<String, String> properties) {
        this.name = trim(name);
        properties.forEach((k, v) -> this.properties.put(trim(k), trim(v)));
    }

    @Override
    public String toString() {
        if (properties.size() == 0) {
            return name;
        }
        return name + "(" + Joiner.on(", ").withKeyValueSeparator(" = ").join(properties) + ")";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Annotation that = (Annotation) o;

        return name != null ? name.equals(that.name) : that.name == null;
    }

    @Override
    public int hashCode() {
        return name != null ? name.hashCode() : 0;
    }
}
